package com.gz.furnitures.web;


import com.gz.furnitures.entity.*;
import com.gz.furnitures.service.impl.OrderServiceImpl;
import com.gz.furnitures.utils.DataUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

@WebServlet(urlPatterns = "/orderServlet")
public class OrderServlet extends BasicServlet{
    private OrderServiceImpl orderService = new OrderServiceImpl();

    protected void saveOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cart cart=(Cart) request.getSession().getAttribute("cart");
        //这里还需要补充一个逻辑，即但购物车在session中，所以购物车不为null，但购物车中的家具为空
        //这里cart.getItems().size()==0可以封装成cart.isEmpty(),我这里没封装
        if(cart==null || cart.getItems().size()==0){ //说明会员没有加入任何家具，转发到首页
            request.getRequestDispatcher("index.jsp").forward(request,response);
            return;
        }

        //获取当前登录的memebrId
        Member member = (Member)request.getSession().getAttribute("member");
        if (member==null){ //说明用户没有登录，转到登录页面
            request.getRequestDispatcher("/views/member/login.jsp").forward(request, response);
            return;
        }

        //上面都满足接下来可以生成订单了
        String orderId = null;
        try {
            orderId = orderService.saveOrder(cart, member.getId());
        } catch (Exception e) {
            //将捕获到的异常抛给了BasicServlet
            throw new RuntimeException(e);
        }
        request.getSession().setAttribute("orderId", orderId);

        response.sendRedirect(request.getContextPath() + "/views/order/checkout.jsp");
    }

    protected void orderPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo = DataUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = DataUtils.parseInt(request.getParameter("pageSize"), Page.DEFAULT_PAGE_SIZE);
        int memberId = DataUtils.parseInt(request.getParameter("memberId"), 0);
        Page<Order> orderPage = orderService.orderPage(pageNo,pageSize,memberId);
        StringBuilder url = new StringBuilder("orderServlet?action=orderPage");
        orderPage.setUrl(url.toString());
        request.setAttribute("orderPage", orderPage);
        request.getRequestDispatcher("/views/order/order.jsp").forward(request,response);
    }

    protected void orderItemPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo = DataUtils.parseInt(request.getParameter("pageNo"), 1);
        int pageSize = DataUtils.parseInt(request.getParameter("pageSize"), Page.DEFAULT_PAGE_SIZE);
        String orderId = request.getParameter("orderId");
        request.setAttribute("orderId", orderId);
        Page<OrderItem> orderItemPage =orderService.orderItemPage(pageNo,pageSize,orderId) ;
        //获得一共多少商品
        int itemCount = orderService.getOrderItemTotalCount(orderId);
        //获得一共多少金额
        BigDecimal itemTotalPrice = orderService.getOrderItemTotalPrice(orderId);
        request.setAttribute("itemTotalPrice", itemTotalPrice);
        request.setAttribute("itemCount", itemCount);
        StringBuilder url = new StringBuilder("orderServlet?action=orderItemPage");
        orderItemPage.setUrl(url.toString());
        request.setAttribute("orderItemPage", orderItemPage);
        request.getRequestDispatcher("/views/order/order_detail.jsp").forward(request,response);
    }
}
